<?php

/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 16/4/7
 * Time: 上午9:34
 */
class AnnouncementController extends AdminBaseController {

    public function index(){
        $this->setTitle('游戏公告列表', '进入游戏时的公告信息');

        $filter = new PubFilter();
        $grid   = new PubGrid();

        $filter->addOssServer()
            ->addFilter('begin', 'input', '创建时间', array('value' => date('Y-m-01'), 'date' => 'day'))
            ->addFilter('end', 'input', '至', array('value' => date('Y-m-d'), 'date' => 'day'));

        $params = $filter->get();

        $header = AnnouncementService::indexHeader();
        $data   = AnnouncementService::indexData($params);
        $grid->addGrid($header, $data, array(
            'order' => array('id', 'desc'),
        ));

        $this->out['filter'] = $filter->show();
        $this->out['grid'] = $grid->show();
    }

    public function add(){
        $this->setTitle('新增游戏公告');

        $this->out['serverList'] = ServerService::getOptions();

        $this->out['typeList'] = BroadcastService::typeList();
    }

    public function doAdd(){
        $server = $this->R('server');
        $params = array(
            'title'             => $this->R('title'),
            'content'           => $this->R('content'),
            'date'              => $this->R('date'),
            'validity'          => $this->R('validity'),
            'itemTitle'         => $this->R('item_title'),
            'itemContent'       => $this->R('item_content'),
            'isPopUpFirstLogin' => $this->R('isPopUpFirstLogin'),
            'isPopUpEveryLogin' => $this->R('isPopUpEveryLogin'),
            'version'           => $this->R('version')
        );

        $result = AnnouncementService::edit($server, $params);

        if($result['state']){
            $this->showMessage('添加成功', PubFunc::buildUrl('/oss/?r=announcement/index'));
        }else{
            $this->showMessage($result['msg'], PubFunc::buildUrl('/oss/?r=announcement/index'));
        }
    }

    public function edit(){
        $id = $this->R('id');
        $info = AnnouncementService::get($id);
        if(empty($info)){
            $this->showMessage('找不到该条游戏公告!');
        }

        $this->setTitle('编辑游戏公告');

        $this->out['info'] = $info;
    }

    public function doEdit(){
        $server = $this->R('server');
        $id     = $this->R('id');
        $params = array(
            'title'             => $this->R('title'),
            'content'           => $this->R('content'),
            'date'              => $this->R('date'),
            'validity'          => $this->R('validity'),
            'itemTitle'         => $this->R('item_title'),
            'itemContent'       => $this->R('item_content'),
            'isPopUpFirstLogin' => $this->R('isPopUpFirstLogin'),
            'isPopUpEveryLogin' => $this->R('isPopUpEveryLogin'),
            'version'           => $this->R('version')
        );

        $result = AnnouncementService::edit($server, $params, $id);

        if($result['state']){
            $this->showMessage('修改成功', PubFunc::buildUrl('/oss/?r=announcement/index'));
        }else{
            $this->showMessage($result['msg'], PubFunc::buildUrl('/oss/?r=announcement/index'));
        }
    }

    public function delete(){
        $id = $this->R('id');
        $result = AnnouncementService::delete($id);
        if($result['state']){
            $msg = '删除成功';
        }else{
            $msg = '删除失败';
        }

        $this->showMessage($msg, $_SERVER['HTTP_REFERER']);
    }

}